package com.A21MobileSales;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.A21.Object.MSTCustomer;
import com.A21.Object.MSTCustomerLocation;
import com.A21.Object.MSTSettings;
import com.A21MobileSales.DataBase.DatabaseHelper;

public class CustomerInfo extends Activity {

	DatabaseHelper ObjDBHelper;
	String Code;
	Button btn_Home;
	ImageButton btn_back;
	TextView tvCode, tvBAdd, tvContact, tvTelephone1, tvTelephone2, tvMobile,
			tvFax, tvEmail, tvCreditLimit, tvCreditPeriod, tvCreditUsed,
			tvPaymentTerm, lblCurrent, tvCurrent, lblOverdue0130,
			tvOverdue0130, lblOverdue3160, tvOverdue3160, lblOverdue61690,
			tvOverdue6190, lblOver90, tvOver90, lblBalance, tvBalance,
			lblOverdue916120, tvOverdue916120, tvCurrency, lblBAdd, tv_CName;

	TableRow rowBucket4;
	TableLayout tblDetail;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_info);
		try {

			if (getIntent().hasExtra("Code")) {
				Code = getIntent().getStringExtra("Code");
			}
			tblDetail = (TableLayout) findViewById(R.id.tblDetail);
			tv_CName = (TextView) findViewById(R.id.tv_CName);
			tvCode = (TextView) findViewById(R.id.tvCode);
			tvBAdd = (TextView) findViewById(R.id.tvBAdd);
			tvContact = (TextView) findViewById(R.id.tvContact);
			tvTelephone1 = (TextView) findViewById(R.id.tvTelephone1);
			tvTelephone2 = (TextView) findViewById(R.id.tvTelephone2);
			tvMobile = (TextView) findViewById(R.id.tvMobile);
			tvFax = (TextView) findViewById(R.id.tvFax);
			tvEmail = (TextView) findViewById(R.id.tvEmail);
			tvCreditLimit = (TextView) findViewById(R.id.tvCreditLimit);
			tvCreditPeriod = (TextView) findViewById(R.id.tvCreditPeriod);
			tvCreditUsed = (TextView) findViewById(R.id.tvCreditUsed);
			tvPaymentTerm = (TextView) findViewById(R.id.tvPaymentTerm);
			lblCurrent = (TextView) findViewById(R.id.lblCurrent);
			tvCurrent = (TextView) findViewById(R.id.tvCurrent);
			lblOverdue0130 = (TextView) findViewById(R.id.lblOverdue0130);
			tvOverdue0130 = (TextView) findViewById(R.id.tvOverdue0130);
			lblOverdue3160 = (TextView) findViewById(R.id.lblOverdue3160);
			tvOverdue3160 = (TextView) findViewById(R.id.tvOverdue3160);
			lblOverdue61690 = (TextView) findViewById(R.id.lblOverdue61690);
			tvOverdue6190 = (TextView) findViewById(R.id.tvOverdue6190);
			lblOver90 = (TextView) findViewById(R.id.lblOver90);
			tvOver90 = (TextView) findViewById(R.id.tvOver90);
			lblBalance = (TextView) findViewById(R.id.lblBalance);
			tvBalance = (TextView) findViewById(R.id.tvBalance);
			tvCurrency = (TextView) findViewById(R.id.tvCurrency);
			lblOverdue916120 = (TextView) findViewById(R.id.lblOverdue916120);
			tvOverdue916120 = (TextView) findViewById(R.id.tvOverdue916120);
			lblBAdd = (TextView) findViewById(R.id.lblBAdd);

			rowBucket4 = (TableRow) findViewById(R.id.rowBucket4);

			btn_Home = (Button) findViewById(R.id.btn_home);
			btn_Home.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent _intent = new Intent();
					_intent.setClass(CustomerInfo.this, HomeScreen.class);
					startActivity(_intent);
				}
			});

			btn_back = (ImageButton) findViewById(R.id.btn_back);
			btn_back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle b = new Bundle();
					Intent _intent = new Intent();
					_intent.putExtras(b);
					_intent.setClass(CustomerInfo.this, CustomerMaster.class);
					startActivity(_intent);
				}
			});

			ObjDBHelper = new DatabaseHelper(this);
			MSTCustomer customer = ObjDBHelper.GetCustomerByCode(Code);
			MSTSettings setting = ObjDBHelper.GetSystemSettingsById("1");

			tvCode.setText(Code);
			String add = "";
			int noOfLines = 0;
			if (customer.Add1 != null && !customer.Add1.isEmpty()) {
				add = customer.Add1;
			}
			if (customer.Add2 != null && !customer.Add2.isEmpty()) {
				if (!add.isEmpty()) {
					add = add + "\n" + customer.Add2;
				} else {
					add = customer.Add2;
				}
				noOfLines++;
			}
			if (customer.Add3 != null && !customer.Add3.isEmpty()) {
				if (!add.isEmpty()) {
					add = add + "\n" + customer.Add3;
				} else {
					add = customer.Add3;
				}
				noOfLines++;
			}

			if (customer.Add4 != null && !customer.Add4.isEmpty()) {
				if (!add.isEmpty()) {
					add = add + "\n" + customer.Add4;
				} else {
					add = customer.Add4;
				}
				noOfLines++;
			}

			if (customer.Pin != null && !customer.Pin.isEmpty()) {
				if (!add.isEmpty()) {
					add = add + "\n" + customer.Pin;
				} else {
					add = customer.Pin;
				}
				noOfLines++;
			}

			String strlblBAdd = "Billing Address";
			for (int l = 0; l < noOfLines; l++) {
				strlblBAdd = strlblBAdd + "\n ";
			}
			DecimalFormat df2 = new DecimalFormat("#0.00");

			tvBAdd.setText(add);
			lblBAdd.setText(strlblBAdd);

			tvContact.setText(customer.Contact);
			tvTelephone1.setText(customer.Telephone1);
			tvTelephone2.setText(customer.Telephone2);
			tvMobile.setText(customer.Mobile);
			tvFax.setText(customer.Fax);
			tvEmail.setText(customer.Email);
			tvCreditLimit.setText(String.valueOf(df2
					.format(customer.CusCreditLimit)));
			tvCreditPeriod.setText(String.valueOf(customer.CusCreditPeriod));
			tvCreditUsed.setText(String.valueOf(df2
					.format(customer.CusCreditUsed)));
			tvPaymentTerm.setText(customer.CusPayTerm);

			tvCurrent.setText(String.valueOf(customer.Bucket0));
			tvOverdue0130.setText(String.valueOf(df2.format(customer.Bucket1)));
			tvOverdue3160.setText(String.valueOf(df2.format(customer.Bucket2)));
			tvOverdue6190.setText(String.valueOf(df2.format(customer.Bucket3)));
			tvOver90.setText(String.valueOf(df2.format(customer.Bucket5)));
			tvBalance
					.setText(String.valueOf(df2.format(customer.BucketBalance)));
			tvOverdue916120
					.setText(String.valueOf(df2.format(customer.Bucket4)));
			tvCurrency.setText(customer.Currency);
			tv_CName.setText(customer.CName);

			if (!setting.SOAtype.equals("D")) {
				lblOverdue0130.setText("1 Month");
				lblOverdue3160.setText("2 Month");
				lblOverdue61690.setText("3 Month");
				rowBucket4.setVisibility(View.VISIBLE);
				lblOverdue916120.setText("4 Month");
				lblOver90.setText("Over 4 Month");
			}
			ArrayList<MSTCustomerLocation> lstLocations = ObjDBHelper
					.GetAllCustomerByCode(Code);
			int addSNO = 0;

			TableRow.LayoutParams paramLeft = new TableRow.LayoutParams(
					TableRow.LayoutParams.WRAP_CONTENT,
					TableRow.LayoutParams.WRAP_CONTENT);
			paramLeft.setMargins(2, 2, 0, 0);

			TableRow.LayoutParams paramRight = new TableRow.LayoutParams(
					TableRow.LayoutParams.WRAP_CONTENT,
					TableRow.LayoutParams.WRAP_CONTENT);
			paramRight.setMargins(2,2,2, 0);

			TableRow.LayoutParams paramBottomLeft = new TableRow.LayoutParams(
					TableRow.LayoutParams.WRAP_CONTENT,
					TableRow.LayoutParams.WRAP_CONTENT);
			paramBottomLeft.setMargins(2, 2, 0, 2);

			TableRow.LayoutParams paramBottomRight = new TableRow.LayoutParams(
					TableRow.LayoutParams.WRAP_CONTENT,
					TableRow.LayoutParams.WRAP_CONTENT);
			paramBottomRight.setMargins(2, 2, 2, 2);

			int cntAdd = lstLocations.size();
			for (MSTCustomerLocation row : lstLocations) {
				addSNO++;
				TextView tvLbl = new TextView(this);
				TextView tvVal = new TextView(this);
				if (cntAdd == addSNO) {
					tvLbl.setLayoutParams(paramBottomLeft);
					tvVal.setLayoutParams(paramBottomRight);
				} else {
					tvLbl.setLayoutParams(paramLeft);
					tvVal.setLayoutParams(paramRight);
				}
				// TextSize_large
				tvLbl.setBackgroundColor(Color.parseColor("#f0f0f0"));
				tvLbl.setTextColor(Color.parseColor("#000000"));
				tvLbl.setPadding(2, 2, 2, 2);
				tvLbl.setTextSize(getResources().getDimension(
						R.dimen.dynTextSize_large));

				tvVal.setBackgroundColor(Color.parseColor("#f0f0f0"));
				tvVal.setTextColor(Color.parseColor("#000000"));
				tvVal.setPadding(2, 2, 2, 2);
				tvVal.setTextSize(getResources().getDimension(
						R.dimen.dynTextSize_large));

				add = "";
				noOfLines = 0;
				if (row.Add1 != null && !row.Add1.isEmpty()) {
					add = row.Add1;
				}
				if (row.Add2 != null && !row.Add2.isEmpty()) {
					if (!add.isEmpty()) {
						add = add + "\n" + row.Add2;
					} else {
						add = row.Add2;
					}
					noOfLines++;
				}
				if (row.Add3 != null && !row.Add3.isEmpty()) {
					if (!add.isEmpty()) {
						add = add + "\n" + row.Add3;
					} else {
						add = row.Add3;
					}
					noOfLines++;
				}

				if (row.Add4 != null && !row.Add4.isEmpty()) {
					if (!add.isEmpty()) {
						add = add + "\n" + row.Add4;
					} else {
						add = row.Add4;
					}
					noOfLines++;
				}

				if (row.Pin != null && !row.Pin.isEmpty()) {
					if (!add.isEmpty()) {
						add = add + "\n" + row.Pin;
					} else {
						add = row.Pin;
					}
					noOfLines++;
				}

				strlblBAdd = "Address " + String.valueOf(addSNO);
				for (int l = 0; l < noOfLines; l++) {
					strlblBAdd = strlblBAdd + "\n ";
				}
				tvLbl.setText(strlblBAdd);
				tvVal.setText(add);
				TableRow tblRow = new TableRow(this);
				tblRow.addView(tvLbl);
				tblRow.addView(tvVal);
				tblDetail.addView(tblRow);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public int convertDpToPixel(float dp) {
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return (int) px;
	}
}